package com.zhouyibike.uu.ui.activity.hehuoren;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.Hehuorenpic;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.activity.mybike.Imagesee.ImagePagerActivity;
import com.zhouyibike.uu.ui.activity.mybike.Imagesee.ImagePagerActivity2;
import com.zhouyibike.uu.ui.adapter.HehuorenjieshaoAdapter;
import com.zhouyibike.uu.ui.base.BaseActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ChengshihehuorenxiangqingActivity extends BaseActivity {
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    private ListView lv;
    private HehuorenjieshaoAdapter adapter;
    private ArrayList<Hehuorenpic.DataBean> list = new ArrayList<>();
    private ArrayList<String> imgs = new ArrayList<>();
    private TextView textView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chengshihehuorenxiangqing);
        initview();
        getMsg();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (imgs != null && imgs.size() > 0) {
                    Intent intent = new Intent(ChengshihehuorenxiangqingActivity.this, ImagePagerActivity2.class);
                    // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, (Serializable) imgs);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, i);
                    startActivity(intent);
                }
            }
        });
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ChengshihehuorenxiangqingActivity.this, ChengshihehuorenbuybikeActivity.class);
                startActivityForResult(intent,200);
            }
        });
    }

    private void initview() {
        textView2 = (TextView) findViewById(R.id.textView2);
        if (getIntent().getBooleanExtra("show",true))
        {
        }else{
            textView2.setVisibility(View.GONE);
        }
        lv = (ListView) findViewById(R.id.danchehehuoren_lv);
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("城市合伙人");
    }

    //单车合伙人pic
    private void getMsg() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
//        showProgressDialog();
        addSubscription(apiStores.CityBikeCompanyInterface(params),
                new ApiCallback<Hehuorenpic>() {
                    @Override
                    public void onSuccess(Hehuorenpic model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            list.clear();
                            imgs.clear();
                            for (int i = 0; i < model.getData().size() - 1; i++) {
                                list.add(model.getData().get(i));
                                imgs.add(model.getData().get(i).getImagePath());
                            }
                            adapter = new HehuorenjieshaoAdapter(ChengshihehuorenxiangqingActivity.this, list);
                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFinish() {
                        if (adapter != null) {
                            lv.setAdapter(adapter);
                        }
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==200&&resultCode==101)
        {
            finish();
        }
    }
}
